package algorithm.util;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.naming.InitialContext;
import javax.sql.DataSource;;

public class DBUtil {
	
	private static DataSource env;

	static{
		  try {
			  env = (DataSource) new InitialContext().lookup("java:comp/env/dbdb");  
		  } catch (Exception e ) { e.printStackTrace();   } 
	}
	
	public 	static Connection getConnection() throws SQLException{
		
		return env.getConnection();
		
	}
	
	public static void close(ResultSet rs){
		
		if(rs != null){
			try {
				rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
	}
	
	public static void close(Statement stmt){
		
		if(stmt != null){
			try {
				stmt.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
	}
	
	public static void close(Connection conn){
		
		if(conn != null){
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
	}
	
}